package eyou.net.httpclient;

import android.app.Application;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

import eyou.net.request.TOkHttpUtils;
import okhttp3.Authenticator;
import okhttp3.Credentials;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.Route;

/**
 * Created by lpc on 2016/11/7.
 * 记得在Manifest里的Application里添加name字段，指定为TApplication
 * android:name="com.example.net.activity.TApplication"
 */

public class TApplication extends Application {

    @Override
    public void onCreate() {
        super.onCreate();
        init();
    }

    private void init() {
        /**
         * 初始化OkHttpClient
         */
        OkHttpClient client = new OkHttpClient.Builder()
                .authenticator(new Authenticator() {
                    @Override public Request authenticate(Route route, Response response) throws IOException {
                        String credential = Credentials.basic("admin@test.eyou.net", "eyouadmin");
                        return response.request().newBuilder()
                                .header("Authorization", credential)
                                .build();
                    }
                })
                .connectTimeout(10*1000L, TimeUnit.MILLISECONDS)
                .readTimeout(10*1000L, TimeUnit.MILLISECONDS)
                .build();
        TOkHttpUtils.initClient(client) ;

    }
}
